package com.lepu.app.fun.mother_circle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.app.application.MyApplication;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.Observer;
import com.core.lib.utils.ObserverManager;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.adapter.AdapterMotherCircle;
import com.lepu.app.fun.mother_circle.bean.BeanMotherCircle;
import com.lepu.app.fun.mother_circle.fragment.FragmentMotherCircle;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class MotherCircleAllActivity extends BaseFragmentActivity implements OnTopbarNewLeftLayoutListener, AsyncRequest, Observer{

	private static final String REQUEST_DATA = "request_data";

	private static final int MSG_DATA_SUCCESS = 10;
	private static final int MSG_DATA_FAIL = 11;
	
	private LinearLayout mLayout = null;
	
	private ListView mListView;
	private AdapterMotherCircle mDataAdapter = null;
	public ArrayList<BeanMotherCircle> mDataList = null;
	
	private static MotherCircleAllActivity mInstance = null;
	
	public static MotherCircleAllActivity getInstance() {
		return mInstance;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInstance = this;
		setContentView(R.layout.mother_circle_all_activity);

		init();
	}

	@Override
	public void onDestroy() {
		ObserverManager.getInstance().removeObserver(ObserverConst.NOTIFY_MOTHER_CIRCLE_ALL_CHANGE);
		
		super.onDestroy();
		mInstance = null;
		
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	private void showHideNoDataLayout(boolean isShow)
	{
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if(noDataLayout != null)
		{
			if(isShow)
			{
				noDataLayout.setVisibility(View.VISIBLE);
				mLayout.setVisibility(View.GONE);
			}else{
				noDataLayout.setVisibility(View.GONE);
				mLayout.setVisibility(View.VISIBLE);
			}
		}
	}
	
	private void showFirstLoadDataLayout(boolean isShow)
	{
		LinearLayout firstLoadDataLayout = (LinearLayout) findViewById(R.id.firstLoadDataLayout);
		if(firstLoadDataLayout != null)
		{
			if(isShow)
			{
				if(mDataList == null || mDataList.size() == 0)
				{
					firstLoadDataLayout.setVisibility(View.VISIBLE);
				}
				
				LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
				noDataLayout.setVisibility(View.GONE);
				
			}else{
				firstLoadDataLayout.setVisibility(View.GONE);
			}
		}
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("所有圈子");
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		mLayout = (LinearLayout) findViewById(R.id.mother_circle_all_layout);
		
		mListView = (ListView) findViewById(R.id.mother_circle_all_listview);
		
		request();
		showFirstLoadDataLayout(true);
		ObserverManager.getInstance().addObserver(ObserverConst.NOTIFY_MOTHER_CIRCLE_ALL_CHANGE, this);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	//请求数据
	@SuppressLint("DefaultLocale")
	public void request() {
		String url = Setting.getCircleListUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			if (MyApplication.getInstance().checkIsLogin()) {
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
				params.put("UserID", loginInfo.UserID);
				params.put("LoginToken", loginInfo.LoginToken);
			} else {
				params.put("UserID", -1);
				params.put("LoginToken", "");
			}
		
		} catch (Exception e) {
		}
		map.put("ht", params);
		
		String temp = String.format("?key=%d", System.currentTimeMillis());
		ApiClient.http_post(url+temp, map, null, this, REQUEST_DATA, true,false,true);
	}

	@SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_DATA_SUCCESS:
				mDataList = (ArrayList<BeanMotherCircle>) msg.obj;
				if (mDataList != null && mDataList.size() > 0) {
					mDataAdapter = new AdapterMotherCircle(MotherCircleAllActivity.this,mDataList,"MotherCircleAllActivity");
					mListView.setAdapter(mDataAdapter);
					
					showHideNoDataLayout(false);
					if(FragmentMotherCircle.getInstance() != null)
					{
						FragmentMotherCircle.getInstance().request();
					}
				} else {
					showHideNoDataLayout(true);
				}
                
				showFirstLoadDataLayout(false);
				break;
			case MSG_DATA_FAIL:

				hideProgressDialog();
				showFirstLoadDataLayout(false);
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_DATA)) {
			ArrayList<BeanMotherCircle> dataArraylist = BeanMotherCircle.parseJsonData((String) data);

			Message msg = mHandler.obtainMessage();
			msg.what = MSG_DATA_SUCCESS;
			msg.obj = dataArraylist;
			mHandler.sendMessage(msg);
		}  
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if (object.equals(REQUEST_DATA)) {
			mHandler.sendEmptyMessage(MSG_DATA_FAIL);
		} 
	}

	@Override
	public void notify(String arg0, Object arg1, Object arg2) {
		request();
		
	}

}
